package com.example.spring_redis;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Set;

/**
 * Created with IntelliJ IDEA
 * Description
 * User：chenshu
 * Date：2024-07-02
 * Time：11:52
 */
@RestController
public class MyController  {
    @Autowired
    private StringRedisTemplate redisTemplate;

    @GetMapping("/testString")
    public String testString() {
        redisTemplate. execute((RedisConnection connection)->{
            //execute要求毁回调方法内返回一个数据
            connection.flushAll();
            return null;
        });

        redisTemplate.opsForValue().set("key", "111");
        redisTemplate.opsForValue().set("key", "222");
        redisTemplate.opsForValue().set("key", "333 ");

        String val = redisTemplate.opsForValue().get("key");
        System.out.println("value: " + val);
        return "OK";
    }

    @GetMapping("/testList")
    public String testList() {
        redisTemplate. execute((RedisConnection connection)->{
             //execute要求毁回调方法内返回一个数据
             connection.flushAll();
             return null;
        });

        redisTemplate.opsForList().leftPush("key", "111");
        redisTemplate.opsForList().leftPush("key", "111");
        redisTemplate.opsForList().leftPush("key", "111");
        List<String> ret = redisTemplate.opsForList().range("key", 0, -1);
        System.out.println(ret);
        return "OK";
    }


    @GetMapping("/testSet")
    public String testSet() {
        redisTemplate.execute((RedisConnection connection)->{
            //execute要求毁回调方法内返回一个数据
            connection.flushAll();
            return null;
        });

        redisTemplate.opsForSet().add("key", "1", "2", "3");
        Set<String> ret = redisTemplate.opsForSet().members("key");
        System.out.println(ret);

        redisTemplate.opsForSet().remove("key", "1", "2");
        ret = redisTemplate.opsForSet().members("key");
        System.out.println(ret);

        return "OK";
    }

    @GetMapping("/testHash")
    public String testHash() {
        redisTemplate.execute((RedisConnection connection)->{
            //execute要求毁回调方法内返回一个数据
            connection.flushAll();
            return null;
        });

        redisTemplate.opsForHash().put("key", "f1", "111");
        redisTemplate.opsForHash().put("key", "f2", "111");
        redisTemplate.opsForHash().put("key", "f3", "111");

        String ret = (String) redisTemplate.opsForHash().get("key", "f1");
        System.out.println("ret: " + ret);

        Boolean exist = redisTemplate.opsForHash().hasKey("key", "f1");
        System.out.println("exist: " + exist);
        exist = redisTemplate.opsForHash().hasKey("key", "f100");
        System.out.println("exist: " + exist);

        return "OK";
    }
}
